Git Kata 4: Merge Conflicts

Learn about the merging of two branches where the differences can't be auto-merged.

Step 1: Modify the file in the main branch#

To append to the file and save:

  • Add (I want ripple chips) to Crispy Chips.
    • Crispy Chips (I want ripple chips)
    • Save the changes.
Appending "I want ripple chips" to the file

This step adds a new comment to the Crispy Chips line.

The command to commit the change to the repository is given below.

The output will be something like this:

Committing the change to the repository

Command's Parameters

Command / Parameter

Description

commit

This creates a new commit in the current branch.

-a

This stages all the modified files prior to the commit.

-m

This sets a commit message from the command line.

"I want Ripple Chips"

This is the commit message.

This step commits the change made to the Crispy Chips line.

The output of the command is:

Output

Message

Meaning

"[master 238996b] I want ripple chips"

This is the branch and abbreviated hash of the commit and the commit message.

"1 file changed, 1 insertion(+), 1 deletion(-)"

This is the number of files changed and the number of insertions and deletions.

e137e9b...
first commit
e137e9b......
28df86f..
Reordered List
28df86f.....
823c7c8..
Another reorder
823c7c8.....
main
main
HEAD
HEAD
newbranch
newbranch
61dd33f..
First line last
61dd33f.....
70c144e..
Loving yogurt!
70c144e.....
90368b0..
Lowfat milk
90368b0.....
dbf84a9..
Merge
dbf84a9.....
906ce69..
I want Ripple chips
906ce69.....
Viewer does not support full SVG 1.1
The visualization shows the new commit on the main branch

Step 2: Modify the file innewbranch#

The command to switch to newbranch is given below.

When we execute the command above, the output will be something like this:

Switching to newbranch

Command's Parameters

Command / Parameter

Description

checkout

This checks out a commit or switches to a branch.

newbranch

This is the branch to checkout.

This command switches to newbranch.

e137e9b...
first commit
e137e9b......
0f8852b..
Reordered List
0f8852b.....
f9b0742..
Another reorder
f9b0742.....
main
main
HEAD
HEAD
newbranch
newbranch
57cf6d2..
First line last
57cf6d2.....
f325972..
Loving yogurt!
f325972.....
90368b0..
Lowfat milk
90368b0.....
9bfd81d..
Merge
9bfd81d.....
a1fe9b1..
I want Ripple chips
a1fe9b1.....
Viewer does not support full SVG 1.1
The visualization now shows that HEAD is pointing to the most recent commit on newbranch

To append in the file and save:

  • Switch to the text editor and reload the file.
  • Add (I’ll take barbecue) to Crispy Chips:
    • Crispy Chips (I’ll take barbecue)
Appending "I'll take barbecue" to the file

This step makes a conflicting change in newbranch to the same Crispy Chips line we modified in the first step. There are now conflicting versions of the same line of the same file between main and newbranch.

The command to commit the change to the repository is given below.

The result will be something like this:

Committing the change to the repository

Command's Parameters

Command / Parameter

Description

commit

This creates a new commit in the current branch.

-a

This stages all the modified files prior to the commit.

-m

This sets a commit message from the command line.

"Barbecue for me"

This is the commit message.

This step commits the change made in newbranch to the Crispy Chips line of storelist.txt.

e137e9b...
first commit
e137e9b......
28df86f..
Reordered List
28df86f.....
823c7c8..
Another reorder
823c7c8.....
main
main
HEAD
HEAD
newbranch
newbranch
61dd33f..
First line last
61dd33f.....
70c144e..
Loving yogurt!
70c144e.....
90368b0..
Lowfat milk
90368b0.....
dbf84a9..
Merge
dbf84a9.....
906ce69..
I want Ripple chips
906ce69.....
73ab0e7..
Barbecue for me
73ab0e7.....
Viewer does not support full SVG 1.1
The change to the “Crispy Chips” line has been committed on newbranch

The command to switch to main is given below.

The result will be something like this:

Switching to the main branch

Command's Parameters

Command / Parameter

Description

checkout

This checks out a commit or switches to a branch.

master

This is the branch to checkout.

This step switches the current branch back to main.

e137e9b...
first commit
e137e9b......
28df86f..
Reordered List
28df86f.....
823c7c8..
Another reorder
823c7c8.....
main
main
HEAD
HEAD
newbranch
newbranch
61dd33f..
First line last
61dd33f.....
70c144e..
Loving yogurt!
70c144e.....
90368b0..
Lowfat milk
90368b0.....
dbf84a9..
Merge
dbf84a9.....
906ce69..
I want Ripple chips
906ce69.....
73ab0e7..
Barbecue for me
73ab0e7.....
Viewer does not support full SVG 1.1
HEAD now refers to the latest commit on main

The command to display the difference between the branches is given below.

After running the command above, the output will be something like this:

Displaying difference between branches

Command's Parameters

Command / Parameter

Description

diff

This displays the difference between objects, including commits, branches, the working tree, and index.

newbranch

This execution of git diff indicates a branch, newbranch. This compares the current branch, main, with newbranch.

Once again, git diff displays the differences between main and newbranch. The storelist.txt file has been modified again, and we see the current differences in the versions between branches.

This output displays two separate difference hunks: the Yummy Yogurt line is still different in the newbranch version, while the other hunk shows the difference we just created in the Crispy Chips line.

The output includes:

Output

Message

Meaning

"@@ -1,5 +1,5 @@"

This is the first difference from the previous step. Five lines, starting from line 1 and ending on line 5, are displayed. `Yummy Yogurt` has the `I love yogurt` comment in main but not in newbranch.

"@@ -13,4 +13,4 @@"

This is the second difference in storelist.txt. It extends four lines from line 13. This is the difference we created on the `Crispy Chips` line. Both versions, main and newbranch, have different text on the same line.

Step 3: Merge and resolve conflicts#

The command to switch to newbranch is given below.

The output will be something like this:

Committing to the repository

Commands

Command / Parameter

Description

merge

This applies commits from one branch to another branch.

newbranch

This is the branch from which we take commits and apply them to the current branch.

The Git merge command attempts to merge newbranch and main. This execution is different from the last git merge in the previous step because the Crispy Chips line has conflicting changes in both branches. Git cannot auto-merge, so the output indicates that there’s a merge conflict.

Reload the file by switching to the text editor and reloading storelist.txt.

File view

The first step in resolving a merge conflict is to review the conflict headers added by Git to the file. Git annotates the file with markers indicating the version of the Crispy Chips line in main (indicated by HEAD, because main is the current branch) and newbranch.

The conflict headers are formatted as follows:

Conflict Headers

Header

Description

<<<<<<< HEAD

The following lines, up to the equals signs, represent the version in HEAD (in this case, that's main).

Crispy Chips (I want ripple chips)

This is the version of the `Crispy Chips` line in main.

=======

This is a separator between the HEAD version and the other version.

Crispy Chips (I’ll take barbecue)

This is the version of the `Crispy Chips` line in newbranch.

<<<<<<< newbranch

This indicates the branch of the version below the equals-line separator.

We remove the conflict headers and combine the comments: Crispy Chips (Ripple chips and barbecue).

Removing the conflicts and combining the comments

The merge conflict is resolved by editing the line with the conflict (a single line in this example, but can be multiple lines). This situation calls for a compromise—the coders will be buying both Ripple and barbecue chips.

The command to commit the change to the repository is given below.

After the execution of the command above, the output will be something like this:

Committing the change to the repository

Command's Parameters

Command / Parameter

Description

commit

This creates a new commit in the current branch.

-a

This stages all the modified files prior to the commit.

-m

This sets a commit message from the command line.

"Ripple and barbecue, everyone is happy!"

This is the commit message.

The git merge operation in the previous step didn’t create a new commit because the merge conflict had to be resolved first. Git marked the file with conflict headers to assist us with making the desired changes. We had to take it from there and edit the file by hand. Once the desired changes were made, they could be committed.

The previous Git merge command didn’t create a commit; however, it did create a pending merge. This Git commit then creates a new merge commit.

e137e9b...
first commit
e137e9b......
28df86f..
Reordered List
28df86f.....
823c7c8..
Another reorder
823c7c8.....
main
main
HEAD
HEAD
newbranch
newbranch
61dd33f..
First line last
61dd33f.....
70c144e..
Loving yogurt!
70c144e.....
90368b0..
Lowfat milk
90368b0.....
dbf84a9..
Merge
dbf84a9.....
906ce69..
I want Ripple chips
906ce69.....
73ab0e7..
Barbecue for me
73ab0e7.....
6f0afe9..
Merge
6f0afe9.....
Viewer does not support full SVG 1.1
The new merge commit is now included in the visualization as the most recent commit

The command to display the repository log in graph format is given below:

The output will be something like this:

Displaying repository logs

Command's Parameters

Command / Parameter

Description

log

This displays the commit history of the current repository.

--graph

This is the option to display the log as a graph.

--pretty=oneline

This is the option to display each commit on a single line.

The git log command is used to view the commit history of a repository.

The --graph and --pretty=oneline options display the history in a vertical graph format, similar to the horizontal graphs we’ve been using to visualize the commit history and branching. If we flip this vertical graph, we can see that it has essentially the same shape as our horizontal visualization:

e137e9b...
first commit
e137e9b......
28df86f..
Reordered List
28df86f.....
823c7c8..
Another reorder
823c7c8.....
main
main
HEAD
HEAD
newbranch
newbranch
61dd33f..
First line last
61dd33f.....
70c144e..
Loving yogurt!
70c144e.....
90368b0..
Lowfat milk
90368b0.....
dbf84a9..
Merge
dbf84a9.....
906ce69..
I want Ripple chips
906ce69.....
73ab0e7..
Barbecue for me
73ab0e7.....
6f0afe9..
Merge
6f0afe9.....
Viewer does not support full SVG 1.1
The new merge commit is now included in the visualization as the most recent commit

Vertical graph representations are commonly used in graphical Git tools to display long histories.

Practice commands#

We’ve given a terminal and a table containing a list of commands discussed in this lesson. Try out these commands after running the terminal, and check out the results!

Step

Command

This appends I want Ripple Chips to the Crispy Chips line in storelist.txt and saves the file.


nano storelist.txt

This commits the change to the repository, setting the commit comment from the command line.

git commit -a -m "I want Ripple Chips"

This switches to newbranch.

git checkout newbranch

  1. This returns to the text editor and reloads the storelist.txt file.
  2. It also appends “I’ll take barbecue” to the Crispy Chips line and saves the file.



nano storelist.txt

This commits the change to the repository, setting the commit comment from the command line.


git commit -a -m "Barbecue for me"

This switches to main.

git checkout master

This displays the differences between main and newbranch.

git diff newbranch

This merges newbranch with main.

git merge newbranch

  1. This returns to the text editor and reloads the storelist.txt file.
  2. It also removes the conflict headers and combines the comments on the Crispy Chips line: Crispy Chips (Ripple chips and barbecue)




nano storelist.txt

This comments the change to the repository, setting the commit comment from the command line.


git commit -a -m "Ripple and barbecue, everyone is happy!"

This displays the repository log in graph format.

git log --graph --pretty=oneline

Terminal 1
Terminal

Click to Connect...

Git Kata 3: Merging

Git Kata 5: Run a Git Server